// +----------------------------------------------------------------------
// | CodeEngine
// +----------------------------------------------------------------------
// | Copyright 艾邦
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: TaoGe <liangtao.gz@foxmail.com>
// +----------------------------------------------------------------------
// | Version: 2.0 2019-08-30 10:32
// +----------------------------------------------------------------------
'use strict';

import Tool from './tool';

/**
 * 核心库函数
 */
export default class Helper {

	constructor( config, storage ) {
		if ( Tool.empty( config ) ) {
			throw new SyntaxError( 'Helper实例化config不能为空' )
		}
		if ( Tool.empty( storage ) ) {
			throw new SyntaxError( 'Helper实例化storage不能为空' )
		}
		this.config = config;
		this.storage = storage;
	}

	static getInstance( config, storage ) {
		if ( !this.instance ) {
			this.instance = new Helper( config, storage );
		}
		return this.instance;
	}

	// 获取授权令牌
	get token() {
		return this.storage.get( this.config.authorize )
	}

	// 设置授权令牌
	set token( value ) {
		return this.storage.get( this.config.authorize, value )
	}

	// 获取用户信息
	get user() {
		return this.storage.get( this.config.user_info );
	}

	// 设置用户信息
	set user( value ) {
		return this.storage.set( this.config.user_info, value )
	}

	// 图片路径(增加域名)
	picUrl( path = '' ) {
		// API接口基址
		const baseURL = this.config.transfer_protocol + '://' + this.config.domain;
		if ( Tool.empty( path ) ) {
			return null
		}
		return baseURL + path;
	}
}
